package studentsinfomanagement.view.teacher;

import studentsinfomanagement.bean.Department;
import studentsinfomanagement.bean.Teacher;
import studentsinfomanagement.dao.DepartmentDAO;
import studentsinfomanagement.dao.TeacherDAO;
import studentsinfomanagement.main.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TeacherHomeView extends JFrame{
    private TeacherHomeView teacherHomeView = null;
    private Main main = null;//逻辑层对象
    private Teacher teacher = null;//显示的学生信息对象

    public TeacherHomeView(Main main, Teacher teacher){
        this.main = main;
        this.teacher = teacher;
        //初始化学生数据
        initTeacher();

        this.setTitle("教师个人界面");//标题
        this.setSize(350,400);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面退出程序
        this.setResizable(false);//大小不可调节

        GridLayout gl = new GridLayout();//网格布局
        gl.setRows(5);
        gl.setColumns(1);
        this.setLayout(gl);

        //界面元素
        JLabel jl_title = new JLabel("教师个人主页");//标题
        jl_title.setFont(new Font("Dialog",1,15));//设置字体

        JLabel jl_name = new JLabel("姓名：");//姓名
        JTextArea jt_name = new JTextArea();
        jt_name.setText(this.teacher.getName());
        jt_name.setEditable(false);

        JLabel jl_sex = new JLabel("性别");//性别
        JTextArea jt_sex = new JTextArea();
        jt_sex.setText(this.teacher.getSex());
        jt_sex.setEditable(false);

        JLabel jl_dep = new JLabel("专业：");//专业
        JTextArea jt_dep = new JTextArea();
        jt_dep.setText(this.teacher.getDepartment());
        jt_dep.setEditable(false);

        JButton btn_edit = new JButton("修改个人信息");//修改个人信息按钮
        btn_edit.setPreferredSize(new Dimension(150,25));
        JButton btn_cs = new JButton("选课中心");//进入选课中心
        btn_cs.setPreferredSize(new Dimension(150,25));

        //标题区域
        JPanel jp_title = new JPanel();
        jp_title.add(jl_title);
        this.add(jp_title);
        //姓名区域
        JPanel jp_name = new JPanel();
        jp_name.add(jl_name);
        jp_name.add(jt_name);
        this.add(jp_name);
        //性别区域
        JPanel jp_sex = new JPanel();
        jp_sex.add(jl_sex);
        jp_sex.add(jt_sex);
        this.add(jp_sex);
        //专业区域
        JPanel jp_dep = new JPanel();
        jp_dep.add(jl_dep);
        jp_dep.add(jt_dep);
        this.add(jp_dep);
        //按钮区域
        JPanel jp_btn = new JPanel();
        jp_btn.add(btn_edit);
        jp_btn.add(btn_cs);
        this.add(jp_btn);

        //设置界面可见
        this.setVisible(true);

        //添加监听器
        //修改个人信息监听器
        btn_edit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new TeacherEditView(main,TeacherHomeView.this.teacher);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //选课中心
        btn_cs.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new TeacherCourseCenter(TeacherHomeView.this.teacher);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });


    }

    /**
     * 初始化学生对象（根据上一个界面设置的学号）
     */
    public void initTeacher(){
       TeacherDAO teacherDAO = TeacherDAO.getInstance();
        teacher = teacherDAO.getTeacherByTno(teacher.getTno());
        //修改专业dno为专业名称
        DepartmentDAO departmentDAO = DepartmentDAO.getInstance();
        Department department = departmentDAO.getDepartmentByDno(teacher.getDepartment());
        teacher.setDepartment(department.getName());
        //修改性别F-女 M-男
        String sex = teacher.getSex();
        if(sex.equals("F"))
            sex = "女";
        else
            sex ="男";
        teacher.setSex(sex);
    }

}
